﻿using System;
using COC.Tools.AI.Motion;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Random = UnityEngine.Random;

public class Steering_Main : MonoBehaviour
{
    float randomValue;
    AIBehave s;
    private Vector3 currMousePos;
    List<Vector3> path = new List<Vector3>();
    List<Vector3> obstacles=new List<Vector3>();
    Transform target;
    public bool showAIRadius=true;
    // Use this for initialization
    void Start()
    {
      // Camera.main.transform.position = new Vector3(0, 20, 0);
       target=GameObject.Find("Target").transform;
       Camera.main.transform.LookAt(target.position);

        calculatePath();

        obstacles=ISteeringDriver.getInstance().obstacles;
        genarateObstacles();

        target.SendMessage("startPath",path);
		const float LvlRadius = 60;
		//create some zombies
        MeshRenderer rendering;
		for (int i = 0; i < 530; ++i)
		{
			Vector3 rndPos = new Vector3(
				Random.Range(-LvlRadius, LvlRadius),
				0,
				Random.Range(-LvlRadius, LvlRadius));

			GameObject zombie = GameObject.CreatePrimitive(PrimitiveType.Cube);
			zombie.name = "Zombie" + i.ToString();
			zombie.transform.position = rndPos;

            zombie.collider.isTrigger = true;
            Debug.Log("------------------->>>>>>" + (Material)Resources.Load(("Demo/Steering/Agent_material"), typeof(Material)));
            zombie.renderer.material = Resources.Load(("Demo/Steering/Agent_material"), typeof(Material)) as Material;
            s = zombie.AddComponent<AIBehave>();
            s.target = target; 
            s.path = path.ToArray();
			//Debug.Log(zombie.transform.position.ToString());
		}
        
    }
   
    void OnDrawGizmos()
    {
        /* int index = 0;
         Gizmos.color = Color.blue;
         if (path.Count >= 10)
         {
             while (index!=path.Count)
             {
                 if(index>0)
                     Gizmos.DrawLine(new Vector3(path[index-1].x,0.3f,path[index-1].z),new Vector3(path[index].x,0.3f,path[index].z));
                 index++;

             }
         }*/

        //iTween.DrawLine(path.ToArray(), Color.yellow);
        if(path.Count>0)
        iTween.DrawPath(path.ToArray(), Color.red);
    }

    void genarateObstacles()
    {
        float times = .01f;
        //float angle = 10;
        Vector3 v3 = Vector3.zero;
        GameObject go;
        float x = 15.0f;
        float angle = 1.0f;
        //float y = 3.0f;
        for (int i = 0; i < 8; i++)
        {
            //v3 = (Quaternion.AngleAxis(Random.Range(-angle, angle), Vector3.up) * new Vector3(Random.Range(-10, 10), 0.3f, Random.Range(-10, 10)));

            v3 = new Vector3(Mathf.Sin(Random.Range(angle, Mathf.PI * 2)) * x, 0.3f, Mathf.Cos(Random.Range(angle, Mathf.PI * 2)) * x);
            go = GameObject.CreatePrimitive(PrimitiveType.Capsule);
            obstacles.Add(v3);
            go.transform.position = new Vector3(v3.x, 0.3f, v3.z);
            go.transform.localScale = Vector3.one * 5;
            go.renderer.material.color = Color.magenta;
            angle++;
            if (Math.Abs(x % 2) < 0)//change arrow
            {
                x--;

                continue;
            }

            x++;
            //times++;

        }

    }

    void calculatePath()
    {
        float times = .01f;
        //float angle = 10;
        Vector3 v3 = Vector3.zero;
        GameObject go;
        float x = 15.0f;
        float angle = 1.0f;
        //float y = 3.0f;
        for (int i = 0; i < 8; i++)
        {
            //v3 = (Quaternion.AngleAxis(Random.Range(-angle, angle), Vector3.up) * new Vector3(Random.Range(-10, 10), 0.3f, Random.Range(-10, 10)));

            v3 = new Vector3(Mathf.Sin(Random.Range(angle, Mathf.PI * 2)) * x, 0.3f, Mathf.Cos(Random.Range(angle, Mathf.PI * 2)) * x);
            path.Add(v3);
            go = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
            go.transform.position = new Vector3(v3.x, 0.3f, v3.z);
            go.renderer.material.color = Color.green;
            angle++;
            if (Math.Abs(x % 2) < 0)//change arrow
            {
                x--;
               
                continue;
            }

            x++;
            //times++;

        }

    }
    // Update is called once per frame
    void Update()
    {
       
    }
}
